package cms.student.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import cms.student.dao.StudentMapper;
import cms.student.entity.Area;
import cms.student.entity.Student;
import cms.student.entity.Subject;
import cms.student.service.StudentService;

@Service
public class StudentServiceImpl implements StudentService {

	@Autowired
	StudentMapper stuMapper;

	@Override
	public PageInfo<Student> list(int page, String name) {
		// TODO Auto-generated method stub
		PageHelper.startPage(page, 3);
		return new PageInfo<Student>(stuMapper.list(name));
	}

	@Override
	public List<Area> listAreasByParentId(int parentId) {
		// TODO Auto-generated method stub
		return stuMapper.listAreasByParentId(parentId);
	}

	@Override
	public List<Subject> listAllSubjects() {
		// TODO Auto-generated method stub
		return stuMapper.listAllSubjects();
	}

	@Override
	public void addStudent(Student stu, int[] selSubId) {
		// TODO Auto-generated method stub
		stuMapper.add(stu);
		for (int subId : selSubId) {
			stuMapper.addStuSub(stu.getId(), subId);
		}

	}

	@Override
	public Student getById(int id) {
		// TODO Auto-generated method stub
		return stuMapper.getById(id);
	}

	@Override
	public void updateStudent(Student stu, int[] selSubId) {
		// TODO Auto-generated method stub
		stuMapper.update(stu);

		stuMapper.delSubjectId(stu.getId());

		for (int i = 0; i < selSubId.length; i++) {
			stuMapper.addStuSub(stu.getId(), selSubId[i]);
		}
	}

	@Override
	public void delBatch(int[] ids) {
		// TODO Auto-generated method stub
		stuMapper.delBathSubjectIds(ids);
		stuMapper.delBath(ids);
	}

}
